﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PM.Services;
using PM.Common;
using PM.Entity;

namespace PM.Site
{
    public partial class AdvisorRegister : System.Web.UI.Page
    {
        private bool editMode = false;

        public bool EditMode
        {
            get { return editMode; }
            set { editMode = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null || Session["UserTypeId"] == null)
            {                
                Response.Redirect("Login.aspx?RequestUrl=" + Request.Url.OriginalString);
            }
            else if (Session["UserTypeId"] != null)
            {
                if (Int32.Parse(Session["UserTypeId"].ToString()) == 0)
                {                    
                        this.Loading();
                        if (Session["AdvisorId"] != null && Session["AdvisorId"] != "")
                        {
                            this.Loading(Session["AdvisorId"].ToString());
                            EditMode = true;
                            Session.Remove("AdvisorId");
                        }
                }                
                else
                {
                    Response.Redirect("Default.aspx?RequestUrl=" + Request.Url.OriginalString);
                }
            }
            else
            {
                Response.Redirect("Login.aspx?RequestUrl=" + Request.Url.OriginalString);
            }
        }

        private void Loading()
        {
            if (!Page.IsPostBack)
            {
                EditMode = false;
                ddlTitleList.DataSource = PersonUtil.GetTitleCollection();
                ddlTitleList.DataBind();
                ddlGender.DataSource = PersonUtil.GetGenderCollection();
                ddlGender.DataBind();
                ddlMartialStatus.DataSource = PersonUtil.GetMartialStatusCollection();
                ddlMartialStatus.DataBind();

            }
        }

        private void Loading(string advisorId)
        {
            if (!Page.IsPostBack)
            {
                Advisor advisor = AdvisorServices.GetAdvisorById(advisorId);
                txbAdvisorId.Text = advisor.AdvisorId;
                ddlTitleList.SelectedValue = advisor.Title;
                txbInitials.Text = advisor.Initials;
                txbOtherNames.Text = advisor.Other_Names;
                txbSurname.Text = advisor.Surname;
                ddlGender.SelectedValue = advisor.Gender;
                ddlMartialStatus.SelectedValue = advisor.MaritalStatus;
                txbDob.Text = advisor.DoB.ToShortDateString();
                txbAddOffice.Text = advisor.AddressOff;
                txbAddRes.Text = advisor.AddressRes;
                txbEmail.Text = advisor.Email;
                txbTelMobile.Text = advisor.TeleMob;
                txbTelOffice.Text = advisor.TeleOff;
                txbTelRes.Text = advisor.TeleRes;
                txbFax.Text = advisor.Fax;
                chkActive.Checked = advisor.Active;
                
            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            Advisor advisor = new Advisor();
            advisor.AdvisorId = txbAdvisorId.Text;
            advisor.Title = ddlTitleList.SelectedValue;
            advisor.Initials = txbInitials.Text;
            advisor.Other_Names = txbOtherNames.Text;
            advisor.Surname = txbSurname.Text;
            advisor.Gender = ddlGender.SelectedValue;
            advisor.MaritalStatus = ddlMartialStatus.SelectedValue;
            DateTime dob;
            DateTime.TryParse(txbDob.Text, out dob);
            advisor.DoB = dob;
            advisor.AddressOff = txbAddOffice.Text;
            advisor.AddressRes = txbAddRes.Text;
            advisor.Email = txbEmail.Text;
            advisor.TeleMob = txbTelMobile.Text;
            advisor.TeleOff = txbTelOffice.Text;
            advisor.TeleRes = txbTelRes.Text;
            advisor.Fax = txbFax.Text;
            advisor.Active = chkActive.Checked;

            if (EditMode)
            {
                AdvisorServices.Update(advisor);
            }
            else
            {
                AdvisorServices.Add(advisor);
            }
            
        }



    }
}
